/**
 * Created by 86155 on 2021/12/28.
 */
//通过事件冒泡 获得元素（+ ， - ）
document.querySelector(".pro-list").onclick = function(event) {

    var ele = event.target;

    if(ele.nodeName == "BUTTON" && ele.innerText == "+" ){
        //1.数量
        var number = ele.previousElementSibling.value;//前一个兄弟元素的值
        console.log("数量：" + number);
        ele.previousElementSibling.value = ++number;

        //2.单价
        var unitPrice = ele.parentElement.previousElementSibling.lastElementChild.innerText;
        console.log("单价：" + unitPrice);
        //3. 小计
        var xioaji = unitPrice * number;

        ele.parentElement.nextElementSibling.lastElementChild.innerText = xioaji;

        zongji()


    } else if(ele.nodeName == "BUTTON" && ele.innerText == "-") {
        //1.数量
        var number = ele.nextElementSibling.value;

        if(number > 1) {
            console.log("数量：" + number);
            ele.nextElementSibling.value = -- number;


            //2. 单价
            var unitPrice = ele.parentElement.previousElementSibling.lastElementChild.innerText;
            console.log("单价：" + unitPrice)

            //3.小计
            var xiaoji = unitPrice * number;
            ele.parentElement.nextElementSibling.lastElementChild.innerText = xiaoji;
          zongji()
        }
    } else if(ele.nodeName == "INPUT" && ele.type == "checkbox"){
        zongji()
    }
}

//全选
document.querySelector(".head-checkbox input").onclick = function() {
    var checkValue = document.querySelector(".head-checkbox input").checked;
    var products = document.querySelectorAll(".goods-left input");
    for(var i= 0 ; i < products.length ; i++) {
        products[i].checked = checkValue;
    }

    //统计总价格
    zongji();
}

function zongji() {
    //总数量
    var totalNumber = 0;

    //总价格
    var totalPrice = 0;


    //1 . 获得所有 已选中checkBox 商品进行统计
    var productsCheckBox = document.querySelectorAll(".goods-left input");

    for(var i = 0 ; i < productsCheckBox.length ; i++) {
        if(productsCheckBox[i].checked) {
            var cartQuality = productsCheckBox[i].parentElement.nextElementSibling.children[1];
            var proNumber = parseInt(cartQuality.children[1].value);//单个商品数量
            console.log("单个商品的数量：" + proNumber);

            totalNumber += proNumber;
            console.log("总数量：" + totalNumber);

            var proxjPrice = cartQuality.children[1].parentElement.nextElementSibling.lastElementChild.innerText;
            console.log("单个商品的小计：" + proxjPrice);

            totalPrice += parseFloat(proxjPrice);

        }
    }
    document.querySelector(".selected-count .number").innerText  = totalNumber;
    document.querySelector(".zongjia").lastElementChild.innerText = totalPrice;

  //  chc
  $(".jiesuan-btn").click(function() {
    var str = totalPrice;
    $(".allPrice").val(str);
    location.href="buyProduct/str?str="+str;
  })

}
zongji();







